Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports Microsoft.VisualBasic.PowerPacks
Namespace BMLERP
	Friend Class frmPUR510
		Inherits System.Windows.Forms.Form
		Dim CustType(3) As Object
		Dim StaOrd(6) As Object
		Dim POType(3) As Object
		Dim OldOHPON As Object
		Dim Per(3) As String
		Dim NewTrans As Byte
		Dim WithEvents rst As ADODB.Recordset
		Dim sql As String
		Dim olddelline, oldSave, oldReview, oldEdit, oldCancel, oldCloseline As Object
		Dim oldClosePO As Boolean


		Private Sub cboOHCNO_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboOHCNO.ClickEvent
			If eventArgs.area = 2 Then
				txtOHCNO.Text = cboOHCNO.BoundText
				txtCMNME.Text = cboOHCNO.CtlText
				cboOHCNO.Visible = False
			End If
		End Sub

		Private Sub cboOHCPO_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboOHCPO.SelectedIndexChanged
			'txtOHVDN.SetFocus
		End Sub

		Private Sub cboOHCUR_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboOHCUR.ClickEvent
			If eventArgs.area = 2 Then
				txtOHCUR.Text = cboOHCUR.BoundText
				txtOHEXR.Text = Find("CCI", "CICCR", " where CICCD='" & txtOHCUR.Text & "'")
			End If
		End Sub

		Private Sub cboOHVDN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboOHVDN.ClickEvent
			If eventArgs.area = 2 Then
				txtOHVDN.Text = cboOHVDN.BoundText
				txtVMNME.Text = cboOHVDN.CtlText
				cboOHVDN.Visible = False
			End If
		End Sub

		Private Sub cboOLITN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboOLITN.ClickEvent
			If eventArgs.area = 2 Then
				txtOLITN.Text = cboOLITN.BoundText
				txtOLITD.Text = cboOLITN.CtlText
				cboOLITN.Visible = False
			End If
		End Sub

		Private Sub cboOLPER_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboOLPER.SelectedIndexChanged
			Select Case cboOLPER.SelectedIndex + 1
				Case 1
					txtOLLAM.Text = CStr(CInt(Val(txtOLODQ.Text) * (Val(txtOLUMP.Text) - (Val(txtOLUMP.Text) * Val(txtOLPDC.Text) / 100))))
					txtOLBLA.Text = CStr(Val(txtOLODQ.Text) * (Val(txtOLUMP.Text) - (Val(txtOLUMP.Text) * Val(txtOLPDC.Text) / 100)) * Val(txtOHEXR.Text))
				Case 2
					txtOLLAM.Text = CStr(CInt(Val(txtOLODW.Text) * (Val(txtOLUMP.Text) - (Val(txtOLUMP.Text) * Val(txtOLPDC.Text) / 100))))
					txtOLBLA.Text = CStr(Val(txtOLODW.Text) * (Val(txtOLUMP.Text) - (Val(txtOLUMP.Text) * Val(txtOLPDC.Text) / 100)) * Val(txtOHEXR.Text))
			End Select
		End Sub

		Private Sub cmdAddnew_Click()
			NewTrans = True
			ISADDING = True
			ISEDITING = True
			WriteToScreen()
			ButtonStatus(("NP"))
			txtOHPON.Focus()
		End Sub

		Private Sub CmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdCancel.Click
			NewTrans = False
			ISADDING = False
			ISEDITING = False
			WriteToScreen()
			ButtonStatus(("cl"))
		End Sub

		Private Sub cmdCloseLine_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCloseLine.Click
			Dim recPOL As ADODB.Recordset
			Dim Unt, Item, Line, WGT, Status As Object
			Line = txtOLPOL.Text
			Item = txtOLITN.Text
			Status = cboOLSTT.SelectedIndex + 1
			If (cboOHPOS.SelectedIndex + 1) = 6 Then
				MsgBox("This Purchase Order had been cancel")
				Exit Sub
			Else
				If Status = 6 Then
					MsgBox("This Item had been deleted")
				Else
					If Status = 5 Then
						MsgBox("This Item had been closed before")
					Else
						CloseLine(Item, Line, WGT, Unt, Status)
					End If
				End If
				ButtonStatus(("clne"))
				WriteToScreen()
			End If
		End Sub

		Private Sub cmdClosePO_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdClosePO.Click
			ClosePO()
			ButtonStatus(("CP"))
			WriteToScreen()
		End Sub

		Private Sub cmddelline_Click()
			Dim vb As Object
			If (txtOHPON.Text <> "") And (txtOLITN.Text <> "") Then
				sql = ""
				sql = "select olstt from poh,pol where pol.olitn='" & Trim(txtOLITN.Text) & "' and olstt='1' and poh.ohpos='1'"
				rst = New ADODB.Recordset
				rst.Open(sql, ConnectString, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
				If rst.RecordCount > 0 Then
					vb = MsgBox(" Do you want delete this line ?", MsgBoxStyle.DefaultButton2 + MsgBoxStyle.OkCancel, "Deleting...")
					If vb = MsgBoxResult.Ok Then
						sql = ""
						sql = "delete from POL where OLPON='" & Trim(txtOHPON.Text) & "' AND  OLITN='" & Trim(txtOLITN.Text) & "'"
						DBconn.Execute(sql)
						WriteToScreen()
					Else
						Exit Sub
					End If
				Else
					MsgBox("Can not delete this line !", MsgBoxStyle.Critical, "Warning")
					Exit Sub
				End If
			Else
				MsgBox("The TextBox Order No or Item No is Empty...!", MsgBoxStyle.Critical, "Warning")
			End If
		End Sub
		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			If TestPOStatus() = False Then
				Exit Sub
			Else
				NewTrans = False
				ISADDING = False
				ISEDITING = True
				'WriteToScreen
				ButtonStatus(("edit"))
			End If
		End Sub

		Private Sub cmdexit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdexit.Click
			Me.Close()
		End Sub

		Private Sub cmdNewItem_Click()
			'NewTrans = True
			If txtOHPON.Text <> "" Then
				ISADDING = True
				ISEDITING = True
				WriteToScreen()
				ButtonStatus(("NL"))
			Else
				MsgBox("Not yet Purchase Order")
			End If
		End Sub

		Private Sub CmdPreview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdPreview.Click
			Dim ReportName As String
			ButtonStatus(("PRE"))
			ReportName = "request.rpt"
			If txtOHPON.Text = "" Or IsDBNull(txtOHPON.Text) Then
				MsgBox("You must chose Purchase Order No.")
				Exit Sub
			Else
				CrystalReport1.ReportFileName = DDAN & "\" & ReportName
				CrystalReport1.RetrieveStoredProcParams()
				CrystalReport1.set_StoredProcParam(0, txtOHPON.Text)
				CrystalReport1.Action = 1
			End If
		End Sub

		Private Sub cmdReview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReview.Click
			'Dim myform As New frmReview
			'myform.Tag = "PUR500"
			'myform.Show()
			frmReview.Tag = "PUR500"
			frmReview.ShowDialog()
			ISEDITING = False
			ISADDING = False
			If frmReview.Tag <> "" Then
				txtOHPON.Text = frmReview.Tag
				frmReview.Close()
				'If myform.Tag <> "" Then
				'txtOHPON.Text = myform.Tag
				'myform.Close()
				WriteToScreen()
				ButtonStatus(("REV"))

			End If
		End Sub
		Sub WriteToScreen()
			Dim i As Object
			Dim StrCV As Object
			If NewTrans Then ' tao moi
				cboOHPOT.SelectedIndex = 0
				txtOHPON.Text = ""
				txtOHPOD.Text = CStr(WorkingDate)
				txtOHRQD.Text = ""
				txtOHCNO.Text = ""
				txtCMNME.Text = ""
				txtOHVDN.Text = ""
				txtVMNME.Text = ""
				cboOHCPO.Text = ""
				cboOHPOS.SelectedIndex = 0
				cboOHCUR.defText = ""
				txtOHCUR.Text = ""
				txtOHEXR.Text = CStr(0)
				txtOHTAM.Text = CStr(0)
				txtOHBTA.Text = CStr(0)
			End If

			Dim recPOL As ADODB.Recordset
			If ISADDING Then ' them moi
				txtOLITN.Text = ""
				txtOLLDT.Text = CStr(WorkingDate)
				txtOLITD.Text = ""
				txtOLPOL.Text = CStr(0)
				txtOLODQ.Text = CStr(0)
				txtOLODW.Text = CStr(0)
				txtOLUMP.Text = CStr(0)
				txtOLLAM.Text = ""
				cboOLPER.Text = ""
				txtOLBLA.Text = ""
				cboOLSTT.SelectedIndex = 0
				StrCV = ""
				txtOLRCQ.Text = CStr(0)
				txtOLRCW.Text = CStr(0)
				txtOLPDC.Text = CStr(0)
			Else 'Sua chua
				'FROM AVM LEFT JOIN IIM ON AVM.VMVDN = IIM.IMVDC
				If txtOLPOL.Text <> "" Then
					recPOL = DBconn.Execute("select POL.*,POH.* from POL LEFT JOIN POH ON POL.OLPON=POH.OHPON where POL.OLPON='" & txtOHPON.Text & "' and OLPOL='" & txtOLPOL.Text & "'")
				Else
					recPOL = DBconn.Execute("select POL.*,POH.* from POL LEFT JOIN POH ON POL.OLPON=POH.OHPON where POL.OLPON='" & txtOHPON.Text & "' Order by POL.OLPOL")
				End If
				If Not recPOL.EOF Then
					With recPOL
						cboOHPOT.SelectedIndex = Val(.Fields("OHPOT").Value) - 1
						txtOHPON.Text = .Fields("OHPON").Value
						OldOHPON = txtOHPON.Text
						txtOHPOD.Text = .Fields("OHPOD").Value
						If IsDBNull(.Fields("OHRQD").Value) Then
							txtOHRQD.Text = ""
						Else
							txtOHRQD.Text = .Fields("OHRQD").Value
						End If
						txtOHCNO.Text = .Fields("OHCNO").Value
						txtCMNME.Text = Find("RCM", "CMNME", " WHERE CMCNO='" & txtOHCNO.Text & "'")
						txtOHVDN.Text = .Fields("OHVDN").Value
						txtVMNME.Text = Find("AVM", "VMNME", " WHERE VMVDN='" & txtOHVDN.Text & "'")
						cboOHCPO.SelectedIndex = Val(.Fields("OHCPO").Value) - 1
						For i = 0 To cboOHPOS.Items.Count - 1
							If VB.Left(VB6.GetItemString(cboOHPOS, i), 1) = .Fields("OHPOS").Value Then
								cboOHPOS.SelectedIndex = i
								Exit For
							End If
						Next i

						cboOHCUR.defText = .Fields("OHCUR").Value
						txtOHCUR.Text = .Fields("OHCUR").Value
						txtOHEXR.Text = .Fields("OHEXR").Value
						txtOHTAM.Text = .Fields("OHTAM").Value
						txtOHBTA.Text = .Fields("OHBTA").Value
						txtOLITN.Text = .Fields("OLITN").Value
						txtOLITD.Text = Find("IIM", "IMITD", "Where IMITN='" & txtOLITN.Text & "'")
						txtOLPOL.Text = .Fields("OLPOL").Value
						txtOLODQ.Text = .Fields("OLODQ").Value
						txtOLODW.Text = .Fields("OLODW").Value
						txtOLUMP.Text = .Fields("OLUMP").Value
						txtOLPDC.Text = .Fields("OLPDC").Value
						txtOLLAM.Text = .Fields("OLLAM").Value
						cboOLPER.SelectedIndex = Val(.Fields("OLPER").Value) - 1
						txtOLBLA.Text = .Fields("OLBLA").Value
						cboOLSTT.SelectedIndex = Val(.Fields("OLSTT").Value) - 1
						txtOLRCQ.Text = .Fields("OLRCQ").Value
						txtOLRCW.Text = .Fields("OLRCW").Value
						txtOLLDT.Text = .Fields("OLLDT").Value
					End With
				End If
			End If
			If Adodc4.EOF Then
				Me.Adodc4 = New Microsoft.VisualBasic.Compatibility.VB6.ADODC
				Adodc4.ConnectionString = ConnectString
				Adodc4.ConnectionTimeout = 10
				Adodc4.CommandType = ADODB.CommandTypeEnum.adCmdText
			End If
			Adodc4.RecordSource = "select POL.*,POH.* from POL " & " LEFT JOIN POH ON POL.OLPON=POH.OHPON " & " where POL.OLPON='" & txtOHPON.Text & "'" & " Order by OLPOL"

			Adodc4.Refresh()
		End Sub



		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			If TestPOStatus() = False Then
				Exit Sub
			Else
				WriteToFile()
				NewTrans = False
				ISADDING = False
				ISEDITING = False
				ButtonStatus(("SV"))
			End If
		End Sub

		Private Sub frmPUR510_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			NewTrans = False
			ISEDITING = False
			ISADDING = False
			OpenFile()
			VB6_AddADODataBinding()
			cboOHCNO.RowSource = ADOBind_Adodc1.DataSource
			cboOHVDN.RowSource = ADOBind_Adodc2.DataSource
			cboOHCUR.RowSource = ADOBind_Adodc5.DataSource
			cboOLITN.RowSource = ADOBind_Adodc3.DataSource
			ButtonStatus(("start"))
			Dim myDataFields As String() = {"OLPOL", "OLITN", "OLITD", "CMNME", "OLODQ", "OLRCQ", "OLODW", "OLRCW", "OLUMP", "OLLAM", "OLBLA", "OLSTT"}
			Dim myDataCaptions As String() = {"line", "Item", "Item Description", "Qty.", "RC. Qty.", "Wgt.", "RC. Wgt.", "Unit price", "Price per", "Line amount", "B line amount", "Status"}
			setDataGridView(myDataCaptions, myDataFields, grdPOL)
		End Sub

		Private Sub grdPOL_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grdPOL.ClickEvent
			If Adodc4.Recordset.AbsolutePosition = -1 Then
				WriteToScreen()
				Exit Sub
			End If
			txtOLPOL.Text = grdPOL.Columns(1).value
			WriteToScreen()
			If SELECTROW = "" Then SELECTROW = 1
			Adodc4.Recordset.AbsolutePosition = SELECTROW
		End Sub
		Private Sub grdPOL_RowColChange(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataGridLib.DDataGridEvents_RowColChangeEvent) Handles grdPOL.RowColChange
			If IsNothing(eventArgs.lastRow) Then
				WriteToScreen()
				Exit Sub
			End If
			SELECTROW = Adodc4.Recordset.Bookmark
		End Sub
		Private Sub txtOHCNO_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHCNO.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("RCM", "CMNME", "WHERE CMCNO='" & txtOHCNO.Text & "'")
				If Result = "" Then
					cboOHCNO.Visible = True
					cboOHCNO.Focus()
				Else
					cboOHCNO.Visible = False
					txtCMNME.Text = Result
					cboOHCPO.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHEXR_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHEXR.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtOLITN.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHPOD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHPOD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtOHRQD.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHPON_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHPON.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As Object
			If KeyAscii = 13 Then
				If Not IsNumeric(txtOHPON.Text) Then
					MsgBox("Input string of Digit", MsgBoxStyle.Critical, "Warning")
					txtOHPON.Focus()
					GoTo EventExitSub
				End If
				If txtOHPON.Text = "" Then
					MsgBox("Not let blank PO")
					GoTo EventExitSub
				Else
					Result = Find("POH", "OHPON", "WHERE OHPON='" & txtOHPON.Text & "'")
					If Result = "" Then
						txtOHPOD.Focus()
					Else
						MsgBox("Doc Number is already exist")
						NewTrans = False
						ISADDING = False
						ISEDITING = False
						WriteToScreen()
						ButtonStatus(("start"))
						txtOHRQD.Focus()
					End If
				End If
			End If
EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHPON_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtOHPON.Leave
			'If Not IsNumeric(txtOHPON.Text) Then
			'MsgBox "Input string of Digit"
			'txtOHPON.SetFocus
			'End If
		End Sub

		Private Sub txtOHRQD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHRQD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtOHCNO.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHVDN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHVDN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("AVM", "VMNME", "WHERE VMVDN='" & txtOHVDN.Text & "'")
				If Result = "" Then
					cboOHVDN.Visible = True
					cboOHVDN.Focus()
				Else
					cboOHVDN.Visible = False
					txtVMNME.Text = Result
					cboOHPOS.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOLITN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLITN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("IIM", "IMITD", "WHERE IMITN='" & txtOLITN.Text & "'")
				If Result = "" Then
					cboOLITN.Visible = True
					cboOLITN.Focus()
				Else
					txtOLITD.Text = Result
					cboOLITN.Visible = False
					txtOLODQ.Focus()
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Sub OpenFile()
			Dim DelStatus As Object
			Dim FieldDel As Object
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc1.RecordSource = "Select * from RCM"
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc2.RecordSource = "Select * from AVM"
			Adodc2.Refresh()

			FieldDel = "IMRID"
			DelStatus = "IZ"
			sql = "Select * from IIM where " & FieldDel & " <>'" & DelStatus & "' "
			Adodc3.ConnectionString = ConnectString
			Adodc3.ConnectionTimeout = 10
			Adodc3.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc3.RecordSource = sql
			Adodc3.Refresh()

			Adodc4.ConnectionString = ConnectString
			Adodc4.ConnectionTimeout = 10
			Adodc4.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc4.RecordSource = "Select POL.*,POH.* from POL " & " LEFT JOIN POH ON POL.OLPON=POH.OHPON where POH.OHPON ='" & txtOHPON.Text & "'"
			Adodc4.Refresh()

			Adodc5.ConnectionString = ConnectString
			Adodc5.ConnectionTimeout = 10
			Adodc5.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc5.RecordSource = "Select * from CCM"
			Adodc5.Refresh()

			' Create array from combobox Custtype,StatusOrder, ValuePer
			CustType(1) = "1 Production"
			CustType(2) = "2 Local Purchasing"
			CustType(3) = "3 Import"
			StaOrd(1) = "1 New"
			StaOrd(2) = "2 Modifine"
			StaOrd(3) = "3 Shop Order"
			StaOrd(4) = "4 Started Product"
			StaOrd(5) = "5 Completed"
			StaOrd(6) = "6 Cancel"
			Per(1) = "Unt"
			Per(2) = "Wgt"
			Per(3) = "All"
			POType(1) = "Product"
			POType(2) = "Local Purchasing"
			POType(3) = "Import"

		End Sub

		Sub ButtonStatus(ByRef tdisplay As Object)
			Dim oldPreview As Object
			Dim BSTT As Object

			If tdisplay = "edit" Then
				oldReview = cmdReview.Enabled
				oldEdit = cmdEdit.Enabled
				oldSave = cmdSave.Enabled
				oldCancel = cmdCancel.Enabled
				oldClosePO = cmdClosePO.Enabled
				oldCloseline = cmdCloseLine.Enabled
			End If

			Select Case LCase(tdisplay)

				Case "start"
					cmdReview.Enabled = True
					CmdPreview.Enabled = False
					cmdEdit.Enabled = False
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmdClosePO.Enabled = False
					cmdCloseLine.Enabled = False
				Case "rev" 'Review
					cmdReview.Enabled = True
					CmdPreview.Enabled = True
					cmdEdit.Enabled = True
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmdClosePO.Enabled = True
					cmdCloseLine.Enabled = True

				Case "edit"	' new purchase order
					cmdReview.Enabled = False
					CmdPreview.Enabled = False
					cmdEdit.Enabled = False
					cmdSave.Enabled = True
					cmdCancel.Enabled = True
					cmdClosePO.Enabled = False
					cmdCloseLine.Enabled = False
				Case "pre" 'Preview Purchase order
					cmdReview.Enabled = True
					CmdPreview.Enabled = True
					cmdEdit.Enabled = True
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmdClosePO.Enabled = True
					cmdCloseLine.Enabled = True
				Case "sv" 'Save
					cmdReview.Enabled = True
					CmdPreview.Enabled = True
					cmdEdit.Enabled = True
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmdClosePO.Enabled = True
					cmdCloseLine.Enabled = True
				Case "cl" 'cancel
					cmdReview.Enabled = oldReview
					CmdPreview.Enabled = oldPreview
					cmdEdit.Enabled = oldEdit
					cmdSave.Enabled = oldSave
					cmdCancel.Enabled = oldCancel
					cmdClosePO.Enabled = True
					cmdCloseLine.Enabled = True
				Case "cp" 'Close purchase order
					cmdReview.Enabled = True
					CmdPreview.Enabled = True
					cmdEdit.Enabled = False
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmdClosePO.Enabled = False
					cmdCloseLine.Enabled = False
				Case "clne"	'Close line
					cmdReview.Enabled = True
					CmdPreview.Enabled = True
					cmdEdit.Enabled = True
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmdClosePO.Enabled = True
					cmdCloseLine.Enabled = True
			End Select

			FramePO.Enabled = NewTrans Or ISEDITING
			FramePOLine.Enabled = ISEDITING
		End Sub

		Sub WriteToFile()
			Dim WGT As Object
			Dim Unt As Object
			Dim txtOHLDT As Object
			Dim reqdte As String
			If NewTrans Then ' nhap moi them vao POH
				txtOHLDT = VB6.Format(Now, "yyyy-mm-dd hh:mm:ss")
				txtOHPOD.Text = VB6.Format(WorkingDate, "yyyy-mm-dd hh:mm:ss")
				If txtOHRQD.Text = "" Then
					reqdte = "null"
				Else
					reqdte = "'" & VB6.Format(txtOHRQD.Text, "yyyy-mm-dd hh:mm:ss") & "'"

				End If

				sql = "Insert into POH (OHLDT,OHPOD,OHRQD,OHRID" & ",OHPON,OHCNO,OHVDN" & ",OHCPO,OHPOS,OHCUR,OHPOT" & ",OHEXR,OHTAM,OHBTA) values " & "('" & txtOHLDT & "','" & txtOHPOD.Text & "'," & reqdte & ",'OM'" & ",'" & txtOHPON.Text & "','" & txtOHCNO.Text & "','" & txtOHVDN.Text & "'" & ",'" & cboOHCPO.SelectedIndex + 1 & "','" & cboOHPOS.SelectedIndex + 1 & "','" & cboOHCUR.CtlText & "','" & cboOHPOT.SelectedIndex + 1 & "'" & "," & txtOHEXR.Text & "," & txtOHTAM.Text & "," & txtOHBTA.Text & ")"
				'MsgBox SQL
				DBconn.Execute(sql)
			End If
			Dim recPOL As ADODB.Recordset
			If ISADDING Then ' nhap moi them vao POl
				recPOL = DBconn.Execute("Select * from POL where OLPON='" & txtOHPON.Text & "' order by OLPOL Desc ")
				If recPOL.EOF Then
					txtOLPOL.Text = CStr(1)
				Else
					txtOLPOL.Text = recPOL.Fields("OLPOL").Value + 1
				End If
				txtOLLDT.Text = VB6.Format(txtOLLDT.Text, "yyyy-mm-dd hh:mm:ss")

				sql = "Insert into POL(OLLDT,OLITN,OLITD,OLRID,OLBLA,OLLUS" & ",OLUMP,OLLAM,OLPER,OLSTT" & ",OLPOL,OLODQ,OLODW,OLPON,OLPDC,OLRCQ,OLRCW) values " & "('" & txtOLLDT.Text & "','" & txtOLITN.Text & "','" & txtOLITD.Text & "','OM'," & txtOLBLA.Text & ",'" & UserLogin & "'" & ",'" & txtOLUMP.Text & "','" & txtOLLAM.Text & "','" & cboOLPER.SelectedIndex + 1 & "','" & cboOLSTT.SelectedIndex + 1 & "'" & ",'" & txtOLPOL.Text & "'," & txtOLODQ.Text & "," & txtOLODW.Text & ",'" & txtOHPON.Text & "'," & txtOLPDC.Text & " ,0,0)"
				'MsgBox SQL
				DBconn.Execute(sql)
				WriteToIIM(txtOLODW, txtOLODQ)
				WriteToITH(txtOLODW, txtOLODQ)
			Else 'Sua chua du lieu

				sql = "Update POH SET  OHLDT='" & txtOHLDT & "',OHPOD='" & VB6.Format(txtOHPOD.Text, "mm/dd/yy") & "',OHRQD='" & VB6.Format(txtOHRQD.Text, "mm/dd/yy") & "'" & ",OHPON='" & txtOHPON.Text & "',OHCNO='" & txtOHCNO.Text & "',OHVDN='" & txtOHVDN.Text & "'" & ",OHCPO='" & cboOHCPO.SelectedIndex + 1 & "',OHPOS='" & VB.Left(VB6.GetItemString(cboOHPOS, cboOHPOS.SelectedIndex), 1) & "',OHCUR='" & cboOHCUR.CtlText & "',OHPOT='" & cboOHPOT.SelectedIndex + 1 & "'" & ",OHEXR=" & txtOHEXR.Text & ",OHTAM=" & txtOHTAM.Text & ",OHBTA=" & txtOHBTA.Text & " WHERE OHPON='" & OldOHPON & "'"
				'MsgBox SQL
				DBconn.Execute(sql)

				recPOL = DBconn.Execute("Select * from POL where OLPON='" & txtOHPON.Text & "' and OLPOL='" & txtOLPOL.Text & "'")
				If txtOLITN.Text <> recPOL.Fields("OLITN").Value Then ' Thay doi item moi
					Unt = Val(txtOLODQ.Text)
					WGT = Val(txtOLODW.Text)
					Unt = IIf(IsNumeric(Unt), Unt, 0)
					WGT = IIf(IsNumeric(WGT), WGT, 0)
					'SQL = "Update IIM set IMOOU=IMOOU - (" & Unt & "),IMOOW= IMOOW - (" & WGT & ") where IMITN='" & recPOL!OLITN & "'"
					'DBconn.Execute SQL
					sql = "Update POL set OLLDT='" & txtOLLDT.Text & "',OLITN='" & txtOLITN.Text & "',OLITD='" & txtOLITD.Text & "',OLRID='OM',OLBLA=" & txtOLBLA.Text & ",OLLUS='" & UserLogin & "'" & ",OLUMP='" & txtOLUMP.Text & "',OLLAM='" & txtOLLAM.Text & "',OLPER='" & cboOLPER.SelectedIndex + 1 & "',OLSTT='" & cboOLSTT.SelectedIndex + 1 & "'" & ",OLPOL='" & txtOLPOL.Text & "',OLODQ =" & txtOLODQ.Text & ",OLODW=" & txtOLODW.Text & ",OLPDC= " & txtOLPDC.Text & " WHERE OLPON='" & txtOHPON.Text & "' AND OLPOL='" & txtOLPOL.Text & "'"
					DBconn.Execute(sql)

				Else
					Unt = Val(txtOLODQ.Text) - recPOL.Fields("OLODQ").Value
					WGT = Val(txtOLODW.Text) - recPOL.Fields("OLODW").Value
					sql = "Update POL set OLLDT='" & VB6.Format(txtOLLDT.Text, "mm/dd/yy") & "',OLITN='" & txtOLITN.Text & "',OLITD='" & txtOLITD.Text & "',OLRID='OM',OLBLA=" & txtOLBLA.Text & ",OLLUS='" & UserLogin & "'" & ",OLUMP='" & txtOLUMP.Text & "',OLLAM='" & txtOLLAM.Text & "',OLPER='" & cboOLPER.SelectedIndex + 1 & "',OLSTT='" & cboOLSTT.SelectedIndex + 1 & "'" & ",OLPOL='" & txtOLPOL.Text & "',OLODQ =" & txtOLODQ.Text & ",OLODW=" & txtOLODW.Text & ",OLRCQ =" & txtOLRCQ.Text & ",OLRCW=" & txtOLRCW.Text & ",OLPDC= " & txtOLPDC.Text & " WHERE OLPON='" & txtOHPON.Text & "' AND OLPOL='" & txtOLPOL.Text & "'"
					'MsgBox SQL
					DBconn.Execute(sql)
				End If
				'WriteToIIM WGT, Unt
				'WriteToITH WGT, Unt
			End If
			recPOL.Close()
			Adodc4.RecordSource = "select POL.*,POH.* from POL " & " LEFT JOIN POH ON POL.OLPON=POH.OHPON " & " where POL.OLPON='" & txtOHPON.Text & "'" & " Order by OLPOL"

			Adodc4.Refresh()
		End Sub

		Private Sub txtOLODW_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLODW.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtOLUMP.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOLODQ_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLODQ.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim RecIIM As ADODB.Recordset
			If KeyAscii = 13 Then
				txtOLODW.Focus()

				RecIIM = DBconn.Execute("Select IMWPU from IIM where IMITN='" & txtOLITN.Text & "'")
				If Not RecIIM.EOF Then
					txtOLODW.Text = CStr(RecIIM.Fields("IMWPU").Value * CDbl(txtOLODQ.Text))
				Else
					txtOLODW.Text = CStr(0)
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub


		Private Sub txtOLPDC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLPDC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then cboOLPER.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOLUMP_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLUMP.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtOLPDC.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Sub WriteToITH(ByRef WGT As Object, ByRef unit As Object)
			Dim txtTHREM As Object
			Dim txtTHREF As Object
			Dim txtTHCVN As Object
			Dim txtTHCST As Object
			Dim txtTHLTN As Object
			Dim txtTHWHC As Object
			Dim txtTHDTE As Object
			Dim txtTHTTC As Object
			Dim txtTHDOC As Object
			Dim txtTHITN As Object
			Dim txtTHTID As Object
			Dim txtTHTSQ As Object
			Dim Unt As Object
			unit = IIf(IsNumeric(unit), unit, 0)
			WGT = IIf(IsNumeric(WGT), WGT, 0)

			If Val(Unt) = 0 And Val(WGT) = 0 Then
				MsgBox("Quantity or Weight must be >0 ")
				Exit Sub
			End If

			'Lay so THTSQ
			txtTHTSQ = GetTSQ(txtOLITN)
			Dim recITH As ADODB.Recordset
			recITH = DBconn.Execute("Select * from ITH Order by THTID DESC")
			If recITH.EOF Then
				txtTHTID = 1
			Else
				txtTHTID = recITH.Fields("THTID").Value + 1
			End If
			recITH.Close()
			txtTHITN = txtOLITN.Text
			txtTHDOC = txtOHPON.Text
			txtTHTTC = "PO"
			txtTHDTE = VB6.Format(txtOLLDT.Text, "yyyy-mm-dd hh:mm:ss")
			txtTHWHC = ""
			txtTHLTN = ""
			txtTHCST = txtOLUMP.Text
			txtTHCVN = txtOHCNO.Text
			txtTHREF = txtOHPON.Text
			txtTHREM = ""
			txtTHDTE = VB6.Format(txtTHDTE, "yyyy-mm-dd hh:mm:ss")
			sql = "Insert into ITH (THITN,THTSQ,THTID,THDOC,THTTC,THDTE" & ",THWHC,THLTN,THQTY,THWGT,THCST" & ",THCVN,THREF,THREM,THLUS,THLDT) values " & "('" & txtTHITN & "'," & txtTHTSQ & "," & txtTHTID & ",'" & txtTHDOC & "','" & txtTHTTC & "','" & txtTHDTE & "'" & ",'" & txtTHWHC & "','" & txtTHLTN & "'," & unit & "," & WGT & "," & txtTHCST & ",'" & txtTHCVN & "'" & ",'" & txtTHREF & "','" & txtTHREM & "','" & UserLogin & "','" & VB6.Format(Now, "yyyy-mm-dd hh:mm:ss") & "')"
			' MsgBox SQL
			DBconn.Execute(sql)

			Adodc1.Refresh()
		End Sub


		Sub WriteToIIM(ByRef WGT As Object, ByRef unit As Object)
			'unit = IIf(IsNumeric(unit), unit, 0)
			'WGT = IIf(IsNumeric(WGT), WGT, 0)
			'SQL = "Update IIM set IMOOU=IMOOU + (" & unit & "),IMOOW= IMOOW + (" & WGT & ") where IMITN='" & txtOLITN & "'"
			DBconn.Execute(sql)
		End Sub

		Sub ClosePO()
			Dim recPOL As ADODB.Recordset
			Dim Unt, Item, Line, WGT, Status As Object
			'Dim recPOH As Recordset
			If (cboOHPOS.SelectedIndex + 1) = 6 Then
				MsgBox("This Purchase order had been delete")
				Exit Sub
			Else

				recPOL = DBconn.Execute("Select * from POL where OLPON='" & txtOHPON.Text & "' and OLSTT<>'6'")
				With recPOL
					Do While Not .EOF
						Line = .Fields("OLPOL").Value
						Item = .Fields("OLITN").Value
						Status = .Fields("OLSTT").Value
						' Chi dong nhung Purchase nao chua hoan thanh
						If Status <> 6 And Status <> 5 Then
							CloseLine(Item, Line, WGT, Unt, Status)
						End If
						.MoveNext()
					Loop
					.Close()
				End With
				MsgBox("update POH set OHPOS=5,OHLUS='" & UserLogin & "' WHERE OHPON='" & txtOHPON.Text & "'")
				DBconn.Execute("update POH set OHPOS=5,OHLUS='" & UserLogin & "' WHERE OHPON='" & txtOHPON.Text & "'")

			End If
		End Sub
		Sub CloseLine(ByRef Item As Object, ByRef Line As Object, ByRef WGT As Object, ByRef Unt As Object, ByRef Status As Object)
			If Status <> 6 And Status <> 5 Then
				MsgBox("update pol set OLSTT=5" & "OLLUS='" & UserLogin & "',OLLDT='" & Now & " WHERE OLPON='" & txtOHPON.Text & "' AND OLPOL='" & Line & "'")
				DBconn.Execute("update pol set OLSTT=5," & "OLLUS='" & UserLogin & "',OLLDT='" & Now & " WHERE OLPON='" & txtOHPON.Text & "' AND OLPOL='" & Line & "'")
			Else
				MsgBox("This line had been finished or deleted")
			End If
		End Sub


		Private Function TestPOStatus() As Object
			Dim recPOH As ADODB.Recordset
			Dim recPOL As ADODB.Recordset
			TestPOStatus = True
			recPOH = DBconn.Execute("SELECT  OHPOS  FROM  POH WHERE OHPON = '" & txtOHPON.Text & "'")
			If recPOH.Fields("OHPOS").Value = 5 Then
				MsgBox("This Purchase Order had finished")
				TestPOStatus = False
				Exit Function
			Else

				recPOL = DBconn.Execute("SELECT  OLSTT  FROM  POL WHERE OLPON = '" & txtOHPON.Text & "'and " & "OLPOL='" & txtOLPOL.Text & "'")
				If recPOH.Fields("OHPOS").Value = 5 Then
					MsgBox(" Item of This Purchase Order had finished")
					TestPOStatus = False
					Exit Function
				End If
			End If
		End Function
	End Class
End Namespace